<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text" id="input">
    <script>
        function jsonp(url,keyword, fn) {
            const strName = "jsonp" + Number((Math.random*900 + 100));
            // 1. 创建script标签
            var myscript = document.createElement("script");
            myscript.src = url + keyword + "&callback=" + strName
            // 2. 加入body
            document.body.appendChild(myscript)

            window[strName] = function(data){
                if(myscript) {
                    document.body.removeChild(myscript)
                }
                fn(data)
            }
        }

        // 防抖
        var timer = null
        input.oninput = function() {
            clearTimeout(timer)
            timer = setTimeout(()=> {
                jsonp("https://suggest.taobao.com/sug?code=utf-8&_ksTS=1640761010428_285&k=1&area=c2c&bucketid=0&q=", input.value, function(data) {
                    console.log(data)
                })
            }, 200)
        }
    </script>
</body>
</html>